//
//  XYBaseButton.m
//  NemoOfficeClient
//
//  Created by gao chenyang on 2019/6/27.
//  Copyright © 2019 ainemo. All rights reserved.
//

#import "XYBaseButton.h"

@interface XYBaseButton()

@end

@implementation XYBaseButton

+ (instancetype)buttonFrame:(CGRect)frame ButtonConfig:(XYBaseButtonConfig *)buttonConfig {
    return [[self alloc] initWithFrame:frame ButtonConfig:buttonConfig];
}

- (instancetype)initWithFrame:(CGRect)frame ButtonConfig:(XYBaseButtonConfig *)buttonConfig {
    if (self = [super initWithFrame:frame]) {
        self.buttonConfig = buttonConfig;
    }
    return self;
}
-(void)setButtonConfig:(XYBaseButtonConfig *)buttonConfig{
    _buttonConfig = buttonConfig;
    [self setTitle:buttonConfig.title forState:UIControlStateNormal];
    if (buttonConfig.selectTitle && buttonConfig.selectTitle.length > 0) {
        [self setTitle:buttonConfig.selectTitle forState:UIControlStateSelected];
    }
    [self setImage:buttonConfig.normalImage forState:UIControlStateNormal];
    [self setImage:buttonConfig.selectImage forState:UIControlStateSelected];
    [self setTitleColor:buttonConfig.titleColor forState:UIControlStateNormal];
    [self setBackgroundImage:buttonConfig.backgroundImage forState:UIControlStateNormal];
    self.titleLabel.font = buttonConfig.titleFont;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat centerX = self.xy_width * 0.5;
    CGFloat centerY = self.xy_height * 0.5;
    CGFloat padding = self.buttonConfig.padding * 0.5;
    
    //先改Size和中心点, 否则会有位置显示问题
    if (self.buttonConfig.imageSize.width !=0 && self.buttonConfig.imageSize.height != 0) {
        self.imageView.xy_y = centerY - self.buttonConfig.imageSize.height * 0.5;
        self.imageView.xy_size = self.buttonConfig.imageSize;
    }
    if (self.buttonConfig.titleSize.width !=0 && self.buttonConfig.titleSize.height != 0) {
        self.titleLabel.xy_height = self.buttonConfig.titleSize.height;
        self.titleLabel.xy_width = self.buttonConfig.titleSize.width;
    }
    CGFloat allWidth = self.imageView.xy_width + self.titleLabel.xy_width;
    CGFloat allHeight = self.imageView.xy_height + self.titleLabel.xy_height;
    
    if (self.buttonConfig.styleType == XYButtonStyleTypeLeft) {
        self.imageView.xy_x = centerX - allWidth * 0.5 - padding;
        self.titleLabel.xy_x = CGRectGetMaxX(self.imageView.frame) + padding * 2;
    } else if (self.buttonConfig.styleType == XYButtonStyleTypeRight) {
        self.titleLabel.xy_x = centerX - allWidth * 0.5 - padding;
        self.imageView.xy_x = CGRectGetMaxX(self.titleLabel.frame) + padding * 2;
    } else if (self.buttonConfig.styleType == XYButtonStyleTypeTop) {
        self.imageView.xy_x = centerX - self.imageView.xy_width * 0.5;
        self.imageView.xy_y = centerY - allHeight * 0.5 - padding;
        self.titleLabel.xy_x = centerX - self.titleLabel.xy_width * 0.5;
        self.titleLabel.xy_y = CGRectGetMaxY(self.imageView.frame) + padding * 2;
    } else if (self.buttonConfig.styleType == XYButtonStyleTypeBottom) {
        self.titleLabel.xy_x = centerX - self.titleLabel.xy_width * 0.5;
        self.titleLabel.xy_y = centerY - allHeight * 0.5 - padding;
        self.imageView.xy_x = centerX - self.imageView.xy_width * 0.5;
        self.imageView.xy_y = CGRectGetMaxY(self.titleLabel.frame) + padding * 2;
    }
    if (self.buttonConfig.titleOffsetTop != 0) {
        self.titleLabel.xy_y = self.buttonConfig.titleOffsetTop;
    }
    if (self.buttonConfig.titleOrigin.x != 0 || self.buttonConfig.titleOrigin.y != 0) {
        self.titleLabel.xy_x = self.buttonConfig.titleOrigin.x;
        self.titleLabel.xy_y = self.buttonConfig.titleOrigin.y;
    }
    if (self.buttonConfig.imageOrigin.x != 0 || self.buttonConfig.imageOrigin.y != 0) {
        self.imageView.xy_x = self.buttonConfig.imageOrigin.x;
        self.imageView.xy_y = self.buttonConfig.imageOrigin.y;
    }
}


@end
